package com.mall.order.services;

import com.mall.order.OrderCoreService;
import com.mall.order.OrderPromoService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dto.*;
import com.mall.shopping.IProductService;
import com.mall.shopping.dto.ProductDetailDto;
import com.mall.shopping.dto.ProductDetailRequest;
import com.mall.shopping.dto.ProductDetailResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @program: cs-mall_35th
 * @description:
 * @author: Weiwei Liu
 * @create: 2021-12-18 17:30
 **/
@Component
@Slf4j
public class OrderPromoServiceImpl implements OrderPromoService {

    @Autowired
    OrderConverter orderConverter;

    @Autowired
    OrderCoreService orderCoreService;

    @Reference(check = false, timeout = 3000)
    IProductService productService;

    @Override
    public CreateSeckillOrderResponse createPromoOrder(CreateSeckillOrderRequest request) {

        // 调用创建普通订单接口
        CreateOrderRequest createOrderRequest = orderConverter.createSeckillRequest2CreateOrderRequest(request);

        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(request.getProductId());
        ProductDetailResponse productDetail = productService.getProductDetail(productDetailRequest);
        ProductDetailDto productDetailDto = productDetail.getProductDetailDto();

        CartProductDto cartProductDto = new CartProductDto();
        cartProductDto.setProductId(request.getProductId());
        cartProductDto.setChecked("true");
        cartProductDto.setLimitNum(1L);
        cartProductDto.setProductNum(1L);
        cartProductDto.setSalePrice(request.getPrice());
        cartProductDto.setProductName(productDetailDto.getProductName());
        cartProductDto.setProductImg(productDetailDto.getProductImageBig());

        ArrayList<CartProductDto> cartProductDtos = new ArrayList<>();
        cartProductDtos.add(cartProductDto);
        createOrderRequest.setCartProductDtoList(cartProductDtos);

        CreateOrderResponse createOrderResponse = orderCoreService.createOrder(createOrderRequest);

        CreateSeckillOrderResponse response = new CreateSeckillOrderResponse();
        response.setCode(createOrderResponse.getCode());
        response.setMsg(createOrderResponse.getMsg());

        log.info("创建秒杀订单，{}", createOrderRequest);

        return response;

    }
}
